//
//  SecViewController.m
//  MixRN
//
//  Created by zsgjs on 2017/2/28.
//  Copyright © 2017年 zsgjs. All rights reserved.
//

#import "SecViewController.h"
#import <React/RCTRootView.h>
#import <React/RCTRootViewDelegate.h>
#define kPushNotification @"pushNotification"

@interface SecViewController () <RCTRootViewDelegate>

@property (nonatomic, strong) NSString *str;

@end

@implementation SecViewController

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(back2prev)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil];
    });
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = true;
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSURL *jsCodeLocation;
//        jsCodeLocation = [NSURL URLWithString:@"http://127.0.0.1/index.ios.jsbundle?platform=ios&dev=true"];
    jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"index.ios" withExtension:@"jsbundle"];
    
    NSDate *currentDate = [NSDate date];
    NSTimeInterval currentTime = [currentDate timeIntervalSince1970];
    RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                        moduleName:@"SecondView" initialProperties:nil
                                                     launchOptions:nil];
    rootView.delegate = self;
    NSDate *completeDate = [NSDate date];
    NSTimeInterval completeTime = [completeDate timeIntervalSince1970];
    NSLog(@"excute time = %f", completeTime - currentTime);
    
    if (rootView) {
        self.view = rootView;
    } else {
        UILabel *label = [[UILabel alloc] init];
        label.text = @"加载数据失败";
        label.backgroundColor = [UIColor lightGrayColor];
        label.frame = (CGRect) {80, 80, {100, 40}};
        [self.view addSubview:label];
    }
    
    [self addNotifacations];
}

- (void)addNotifacations
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(failedLoadJsNotification:) name:RCTJavaScriptDidFailToLoadNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadedJsNotification:) name:RCTJavaScriptDidLoadNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backNotification:) name:kPushNotification object:nil];
}

- (void)loadedJsNotification:(NSNotification *)noti
{
    NSString *failedWarn = @"加载reactnative完成";
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:failedWarn message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAct = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
    [alert addAction:sureAct];
    [self presentViewController:alert animated:true completion:nil];
}

- (void)failedLoadJsNotification:(NSNotification *)noti
{
    NSString *failedWarn = @"加载reactnative组件失败";
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:failedWarn message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAct = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
    [alert addAction:sureAct];
    [self presentViewController:alert animated:true completion:nil];
}

- (void)backNotification:(NSNotification *)noti
{
    [self.navigationController popViewControllerAnimated:true];
}

- (void)rootViewDidChangeIntrinsicSize:(RCTRootView *)rootView
{
    
}

@end
